package com.zzyl.nursing.service.impl;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.framework.web.service.TokenService;
import com.zzyl.nursing.dto.AlertDataDto;
import com.zzyl.nursing.dto.AlertDataDtoo;
import com.zzyl.nursing.service.INursingElderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
/**
 * 报警数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-25
 */
@Service
@Slf4j
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper,AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     * 
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData)
    {
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData)?1:0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData)?1:0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))?1:0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id)?1:0;
    }

    /**
     * 分页查询报警数据
     * @param dto 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> listByPage(AlertDataDto dto) {
        Long userId = SecurityUtils.getUserId();

        LambdaQueryWrapper<AlertData> lqw = new LambdaQueryWrapper<>();
        lqw.like(dto.getDeviceName()!=null,AlertData::getDeviceName,dto.getDeviceName());
        lqw.eq(dto.getId()!=null,AlertData::getId,dto.getId());
        // 支持查询未处理和已处理的数据
        if (dto.getStatus() != null) {
            lqw.eq(AlertData::getStatus, dto.getStatus());
        }
        lqw.between(dto.getStartTime()!=null&&dto.getEndTime()!=null,AlertData::getProcessingTime,dto.getStartTime(),dto.getEndTime());
        if (userId != 1) {
            lqw.eq(AlertData::getUserId, userId);
        }
        return list(lqw);
    }

    /**
     * 处理报警数据
     * @param alertData 报警数据
     * @return 处理结果
     */
    @Override
    public int handleAlertData(AlertDataDtoo alertData) {
        LambdaQueryWrapper<AlertData> lqw = new LambdaQueryWrapper<>();
        lqw.eq(alertData.getId()!=null,AlertData::getId,alertData.getId());
        AlertData data = new AlertData();
        data.setProcessingResult(alertData.getProcessingResult());
        LocalDateTime processingTime = alertData.getProcessingTime();
        // 先解折为LocalDateTime，再关联UTC时区转换为Instant
        Instant instant = processingTime.toInstant(ZoneOffset.UTC);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.of("Asia/Shanghai"));
        data.setProcessingTime(localDateTime);
        data. setStatus(1);
        return update( data,lqw)?1:0;
    }

}
